import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;

public class DELETE extends Contronller{
    public static void Delete() {
        AnchorPane pane = new AnchorPane();
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Delete Staff");
        pane.setPrefSize(400, 300);
        Label label = new Label("请输入所删除的员工姓名");
        TextField textField = new TextField();
        pane.getChildren().add(label);
        pane.getChildren().add(textField);
       AnchorPane.setTopAnchor(label, 75.0);
       AnchorPane.setTopAnchor(textField, 75.0);
       AnchorPane.setLeftAnchor(label, 25.0);
       AnchorPane.setLeftAnchor(textField, 175.0);
        Button button1 = new Button("返回");
        Button button2 = new Button("确定");
        pane.getChildren().add(button1);
        pane.getChildren().add(button2);
        AnchorPane.setTopAnchor(button1, 225.0);
        AnchorPane.setTopAnchor(button2, 225.0);
        AnchorPane.setLeftAnchor(button1, 45.0);
        AnchorPane.setLeftAnchor(button2, 225.0);
        button1.setOnAction(e -> {stage.close();});
        button2.setOnAction(e -> {
            Main.staffList.forEach(staff -> {
                int index = Main.staffList.indexOf(staff);
                if (textField.getText().equals(staff.getName())) {
                    Main.staffList.remove(staff);
                    stage.close();
                    StackPane stackPane = new StackPane();
                    Scene stackScene = new Scene(stackPane);
                    Stage stackStage = new Stage();
                    stackStage.setScene(stackScene);
                    stackStage.setTitle("删除成功");
                    stackPane.setPrefSize(300, 100);
                    Button button3 = new Button("确定");
                    stackPane.getChildren().add(button3);
                    button3.setOnAction(actionEvent -> {stackStage.close();});
                    stackStage.show();
                    try {
                        SAVERESOURCE.SaveResource();
                    } catch (IOException f) {
                        throw new RuntimeException(f);
                    }
                }
                if (!(textField.getText().equals(staff.getName()))&&index==Main.staffList.size()-1&&stage.isShowing()) {
                    stage.close();
                    GridPane gridPane = new GridPane();
                    Scene scene2 = new Scene(gridPane);
                    Stage stage2 = new Stage();
                    stage2.setScene(scene2);
                    stage2.setTitle("错误");
                    gridPane.setPrefSize(300, 100);
                    gridPane.setVgap(20);
                    gridPane.setHgap(10);
                    Button button = new Button("确定");
                    gridPane.add(button, 5, 5);
                    button.setOnAction(actionEvent -> {stage2.close();});
                    stage2.show();
                }

            });
        });
        stage.show();
    }
}
